﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using log4net;

namespace Tireguit.Nmon.Parser
{
    public class FileParser:IDisposable
    {
        protected ILog _log=LogManager.GetLogger(typeof(FileParser));
        protected bool disposed = false;
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (DatabaseManager!=null)
                    {
                        DatabaseManager.Dispose();
                        DatabaseManager = null;
                    }
                    if (_streamreader!=null)
                    {
                        _streamreader.Dispose();
                        _streamreader = null;
                    }
                    
                }
                disposed = true;
            }
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        ~FileParser()
        {
            Dispose(false);
        }
        public string FileName { get; protected set; }
        public string ConnectString { get; protected set; }
        public IDbConnection Connection { get; protected set; }
        public Database.DatabaseManager DatabaseManager { get; protected set; }
        protected StreamReader _streamreader = null;
        public FileParser(string nomFichier,string connectstring)
        {
            try
            {
                _log.Debug("start Construct");
                FileName = nomFichier;
                ConnectString = connectstring;
                Connection = new SqlConnection(connectstring);
                DatabaseManager = new Database.DatabaseManager(Connection);
            }
            catch (System.Exception ex)
            {
                _log.Error("Construct", ex);
                throw ex;
            }
            finally
            {
                _log.Debug("stop Construct");
            }
          
        }
        public FileParser(string nomFichier, IDbConnection connection)
        {
            try
            {
                _log.Debug("start Construct");
                FileName = nomFichier;
                ConnectString = connection.ConnectionString;
                Connection = connection;
                DatabaseManager = new Database.DatabaseManager(Connection);
            }
            catch (System.Exception ex)
            {
                _log.Error("Construct", ex);
                throw ex;
            }
            finally
            {
                _log.Debug("stop Construct");
            }

        }
        public bool DoParsing()
        {
            try
            {
                _log.Debug("start Parsing");
                if (!File.Exists(FileName))
                {
                    _log.Error("File not found");
                    return false;
                }
                if(!DatabaseManager.Open())
                {
                    _log.Error("Can't open Database");
                    return false;
                }
                if (!DatabaseManager.CleanDatabase())
                {
                    _log.Error("Can't clean Database");
                    return false;
                }
                _streamreader = new StreamReader(FileName);
                while (!_streamreader.EndOfStream)
                {
                    DatabaseManager.InsertInto(_streamreader.ReadLine());
                }
                _streamreader.Close();
                DatabaseManager.Close();
                return true;
            }
            catch (System.Exception ex)
            {
                _log.Error("DoParsing", ex);
                return false;
            	
            }
            finally
            {
                _log.Debug("stop Parsing");
            }
        }

    }
}
